Load all required libraries.

library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.3     v dplyr   1.0.7
## v tidyr   1.1.3     v stringr 1.4.0
## v readr   2.0.0     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(broom)

Read in raw data from RDS.

raw_data <- readRDS("./n1_n2_cleaned_cases.rds")

Make a few small modifications to names and data for visualizations.

final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
  rename(Facility = wrf) %>%
  mutate(Facility = recode(Facility, 
                           "NO" = "WRF A",
                           "MI" = "WRF B",
                           "CC" = "WRF C"))

Seperate the data by gene target to ease layering in the final plot

#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>% 
  select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
  group_by(date) %>% summarise_if(is.numeric, mean)

#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]

only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]

Build the main plot

      #first layer is the background epidemic curve
        p1 <- only_background %>%
              plotly::plot_ly() %>%
              plotly::add_trace(x = ~date, y = ~new_cases_clarke, 
                                type = "bar", 
                                hoverinfo = "text",
                                text = ~paste('</br> Date: ', date,
                                                     '</br> Daily Cases: ', new_cases_clarke),
                                alpha = 0.5,
                                name = "Daily Reported Cases",
                                color = background_color,
                                colors = background_color,
                                showlegend = FALSE) %>%
            layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #renders the main plot layer two as seven day moving average
        p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke, 
                             type = "scatter",
                             mode = "lines",
                             hoverinfo = "text",
                            text = ~paste('</br> Date: ', date,
                                                     '</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
                             name = "Seven Day Moving Average Athens",
                             line = list(color = seven_day_ave_color),
                             showlegend = FALSE)
      

        
        #renders the main plot layer three as positive target hits
        
        p2 <- plotly::plot_ly() %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n1,
                                       symbol = ~Facility,
                                       marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n2,
                                       symbol = ~Facility,
                                       marker = list(color = '#D95F02', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
            layout(yaxis = list(title = "SARS CoV-2 Copies/L", 
                                 showline = TRUE,
                                 type = "log",
                                 dtick = 1,
                                 automargin = TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #adds the limit of detection dashed line
        p2 <- p2 %>% plotly::add_segments(x = as.Date("2020-03-14"), 
                                          xend = ~max(date + 10), 
                                          y = 3571.429, yend = 3571.429,
                                          opacity = 0.35,
                                          line = list(color = "black", dash = "dash")) %>%
          layout(annotations = list(x = as.Date("2020-03-28"), y = 3.8, xref = "x", yref = "y", 
                                    text = "Limit of Detection", showarrow = FALSE))

        

        p1
        p2

Combine the two main plot pieces as a subplot

#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")

#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")


#rejoin the old data frames then seperate in to averages for each plant. 
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "day", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "day", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "day", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)

Build loess smoothing figures figures

This makes the individual plots

#**************************************WRF A PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.6, n = 422)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'

fit_botha
##   [1] 12.99526 12.99250 12.98977 12.98706 12.98437 12.98171 12.97907 12.97645
##   [9] 12.97386 12.97130 12.96877 12.96626 12.96378 12.96134 12.95892 12.95654
##  [17] 12.95418 12.95187 12.94958 12.94733 12.94511 12.94293 12.94079 12.93869
##  [25] 12.93662 12.93459 12.93261 12.93066 12.92875 12.92689 12.92507 12.92329
##  [33] 12.92156 12.91988 12.91824 12.91664 12.91510 12.91360 12.91215 12.91076
##  [41] 12.90941 12.90811 12.90687 12.90569 12.90457 12.90353 12.90254 12.90162
##  [49] 12.90077 12.89997 12.89923 12.89855 12.89792 12.89734 12.89682 12.89635
##  [57] 12.89592 12.89554 12.89520 12.89491 12.89466 12.89445 12.89428 12.89414
##  [65] 12.89404 12.89397 12.89393 12.89392 12.89394 12.89398 12.89405 12.89414
##  [73] 12.89425 12.89439 12.89454 12.89470 12.89488 12.89507 12.89528 12.89549
##  [81] 12.89571 12.89593 12.89616 12.89639 12.89663 12.89692 12.89733 12.89786
##  [89] 12.89849 12.89923 12.90007 12.90100 12.90202 12.90313 12.90432 12.90558
##  [97] 12.90691 12.90831 12.90976 12.91128 12.91284 12.91445 12.91610 12.91779
## [105] 12.91951 12.92125 12.92302 12.92480 12.92660 12.92841 12.93021 12.93202
## [113] 12.93381 12.93560 12.93737 12.93911 12.94083 12.94252 12.94417 12.94578
## [121] 12.94735 12.94886 12.95032 12.95172 12.95305 12.95432 12.95551 12.95662
## [129] 12.95812 12.96043 12.96347 12.96715 12.97139 12.97610 12.98119 12.98658
## [137] 12.99219 12.99792 13.00370 13.00944 13.01505 13.02045 13.02555 13.03027
## [145] 13.03452 13.03822 13.04128 13.04361 13.04514 13.04697 13.05021 13.05473
## [153] 13.06041 13.06714 13.07478 13.08322 13.09234 13.10202 13.11213 13.12255
## [161] 13.13316 13.14384 13.15448 13.16494 13.17510 13.18486 13.19407 13.20263
## [169] 13.21041 13.21729 13.22315 13.22787 13.23132 13.23339 13.23395 13.23364
## [177] 13.23315 13.23249 13.23165 13.23063 13.22941 13.22800 13.22639 13.22457
## [185] 13.22255 13.22030 13.21784 13.21515 13.21222 13.20906 13.20566 13.20201
## [193] 13.19811 13.19395 13.18953 13.18484 13.17988 13.17464 13.16912 13.16331
## [201] 13.15720 13.15080 13.14409 13.13708 13.12975 13.12139 13.11139 13.09990
## [209] 13.08707 13.07305 13.05798 13.04202 13.02532 13.00801 12.99026 12.97220
## [217] 12.95400 12.93579 12.91773 12.89997 12.88265 12.86592 12.84994 12.83485
## [225] 12.82079 12.80793 12.79459 12.77917 12.76185 12.74286 12.72239 12.70064
## [233] 12.67784 12.65417 12.62985 12.60508 12.58007 12.55502 12.53013 12.50562
## [241] 12.48169 12.45854 12.43638 12.41542 12.39586 12.37790 12.36175 12.34648
## [249] 12.33103 12.31542 12.29968 12.28382 12.26787 12.25185 12.23579 12.21969
## [257] 12.20359 12.18750 12.17145 12.15546 12.13955 12.12375 12.10807 12.09253
## [265] 12.07716 12.06199 12.04702 12.03228 12.01780 12.00360 11.98969 11.97611
## [273] 11.96286 11.95021 11.93836 11.92723 11.91677 11.90691 11.89759 11.88874
## [281] 11.88029 11.87219 11.86437 11.85677 11.84931 11.84194 11.83460 11.82721
## [289] 11.81972 11.81205 11.80415 11.79595 11.78739 11.77840 11.76890 11.75889
## [297] 11.74844 11.73764 11.72657 11.71529 11.70388 11.69243 11.68101 11.66969
## [305] 11.65855 11.64768 11.63714 11.62701 11.61737 11.60829 11.59986 11.59215
## [313] 11.58523 11.57919 11.57409 11.56904 11.56316 11.55656 11.54937 11.54171
## [321] 11.53370 11.52547 11.51712 11.50879 11.50060 11.49266 11.48510 11.47803
## [329] 11.47158 11.46588 11.46103 11.45717 11.45441 11.45287 11.45268 11.45396
## [337] 11.45682 11.46052 11.46425 11.46806 11.47199 11.47608 11.48038 11.48492
## [345] 11.48976 11.49493 11.50048 11.50645 11.51288 11.51982 11.52731 11.53539
## [353] 11.54410 11.55350 11.56361 11.57449 11.58617 11.59838 11.61082 11.62350
## [361] 11.63646 11.64970 11.66325 11.67714 11.69138 11.70600 11.72101 11.73644
## [369] 11.75231 11.76864 11.78545 11.80277 11.82061 11.83899 11.85795 11.87749
## [377] 11.89764 11.91842 11.93985 11.96196 11.98462 12.00770 12.03120 12.05514
## [385] 12.07951 12.10433 12.12960 12.15533 12.18153 12.20820 12.23534 12.26297
## [393] 12.29109 12.31971 12.34883 12.37846 12.40861 12.43928 12.47049 12.50223
## [401] 12.53451 12.56739 12.60091 12.63506 12.66983 12.70520 12.74116 12.77769
## [409] 12.81479 12.85244 12.89063 12.92934 12.96856 13.00828 13.04848 13.08916
## [417] 13.13030 13.17188 13.21389 13.25632 13.29916 13.34239
#assign fits to a vector
both_trenda <- fit_botha

#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax

#reassign dataframes (just to be safe)
work_botha <- wrfa_both

#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date

#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
                    data = smooth_frame_botha,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha,
                                  '</br> Median Log Copies: ', round(both_trenda, digits = 2)),
                    line = list(color = '#1B9E77', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
                                  '</br> Min Log Copies: ', round(both_ymina, digits = 2)),
                    name = "",
                    fillcolor = '#1B9E77',
                    line = list(color = '#1B9E77')) %>%
                layout(yaxis = list(title = "Total Log SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF A") %>%
    plotly::add_segments(x = as.Date("2020-06-24"), 
                                          xend = as.Date("2020-06-24"), 
                                          y = ~min(both_ymina), yend = ~max(both_ymaxa),
                                          opacity = 0.35,
                                          name = "Bars Repoen",
                                          hoverinfo = "text",
                                          text = "</br> Bars Reopen",
                                                 "</br> 2020-06-24",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-07-09"), 
                                          xend = as.Date("2020-07-09"), 
                                          y = ~min(both_ymina), yend = ~max(both_ymaxa),
                                          opacity = 0.35,
                                          name = "Mask Mandate",
                                          hoverinfo = "text",
                                          text = "</br> Mask Mandate",
                                                 "</br> 2020-07-09",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-08-20"), 
                                          xend = as.Date("2020-08-20"), 
                                          y = ~min(both_ymina), yend = ~max(both_ymaxa),
                                          opacity = 0.35,
                                          name = "</br> Classes Begin",
                                                 "</br> 2020-08-20",
                                          hoverinfo = "text",
                                          text = "Classes Begin",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
        plotly::add_segments(x = as.Date("2020-10-03"), 
                                          xend = as.Date("2020-10-03"), 
                                          y = ~min(both_ymina), yend = ~max(both_ymaxa),
                                          opacity = 0.35,
                                          name = "</br> First Home Football Game",
                                                 "</br> 2020-10-03",
                                          hoverinfo = "text",
                                          text = "First Home Football Game",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfa_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#1B9E77', size = 6, opacity = 0.65))

p_wrf_a
save(p_wrf_a, file = "./plotly_objs/p_wrf_a.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02', 
              span = 0.6, n = 422)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'

fit_bothb
##   [1] 12.49460 12.49498 12.49537 12.49576 12.49617 12.49660 12.49704 12.49750
##   [9] 12.49797 12.49847 12.49898 12.49952 12.50009 12.50068 12.50130 12.50195
##  [17] 12.50262 12.50334 12.50408 12.50486 12.50568 12.50653 12.50743 12.50837
##  [25] 12.50935 12.51037 12.51145 12.51257 12.51373 12.51495 12.51623 12.51755
##  [33] 12.51894 12.52037 12.52187 12.52343 12.52505 12.52673 12.52848 12.53029
##  [41] 12.53218 12.53413 12.53615 12.53825 12.54042 12.54266 12.54497 12.54736
##  [49] 12.54980 12.55231 12.55488 12.55751 12.56020 12.56294 12.56573 12.56857
##  [57] 12.57145 12.57439 12.57736 12.58037 12.58343 12.58651 12.58964 12.59279
##  [65] 12.59597 12.59917 12.60241 12.60566 12.60893 12.61222 12.61552 12.61884
##  [73] 12.62216 12.62550 12.62884 12.63218 12.63552 12.63887 12.64221 12.64554
##  [81] 12.64886 12.65218 12.65548 12.65876 12.66203 12.66541 12.66902 12.67285
##  [89] 12.67689 12.68115 12.68560 12.69024 12.69507 12.70007 12.70523 12.71056
##  [97] 12.71604 12.72166 12.72741 12.73329 12.73929 12.74540 12.75162 12.75792
## [105] 12.76432 12.77079 12.77734 12.78394 12.79061 12.79731 12.80406 12.81084
## [113] 12.81764 12.82445 12.83128 12.83810 12.84491 12.85170 12.85847 12.86520
## [121] 12.87190 12.87854 12.88513 12.89165 12.89809 12.90446 12.91074 12.91691
## [129] 12.92299 12.92895 12.93478 12.94049 12.94606 12.95149 12.95676 12.96187
## [137] 12.96681 12.97157 12.97615 12.98053 12.98472 12.98869 12.99244 12.99597
## [145] 12.99927 13.00232 13.00512 13.00829 13.01238 13.01730 13.02294 13.02920
## [153] 13.03600 13.04324 13.05081 13.05861 13.06656 13.07455 13.08249 13.09027
## [161] 13.09781 13.10499 13.11173 13.11793 13.12349 13.12831 13.13230 13.13535
## [169] 13.13737 13.13827 13.13852 13.13868 13.13873 13.13867 13.13850 13.13820
## [177] 13.13777 13.13720 13.13648 13.13560 13.13457 13.13337 13.13199 13.13042
## [185] 13.12867 13.12672 13.12456 13.12219 13.11959 13.11677 13.11372 13.11042
## [193] 13.10687 13.10307 13.09900 13.09466 13.09004 13.08513 13.07993 13.07443
## [201] 13.06862 13.06249 13.05604 13.04927 13.04169 13.03289 13.02294 13.01187
## [209] 12.99976 12.98665 12.97260 12.95768 12.94192 12.92540 12.90816 12.89026
## [217] 12.87177 12.85272 12.83319 12.81323 12.79289 12.77223 12.75130 12.73017
## [225] 12.70889 12.68750 12.66608 12.64468 12.62335 12.60214 12.58112 12.56035
## [233] 12.53987 12.51974 12.50002 12.48077 12.46204 12.44388 12.42637 12.40954
## [241] 12.39345 12.37817 12.36375 12.35025 12.33771 12.32582 12.31419 12.30281
## [249] 12.29166 12.28072 12.26997 12.25939 12.24896 12.23866 12.22848 12.21840
## [257] 12.20840 12.19845 12.18854 12.17866 12.16878 12.15888 12.14894 12.13895
## [265] 12.12889 12.11873 12.10847 12.09807 12.08807 12.07895 12.07063 12.06303
## [273] 12.05606 12.04966 12.04373 12.03820 12.03300 12.02803 12.02322 12.01850
## [281] 12.01377 12.00896 12.00400 11.99879 11.99327 11.98734 11.98094 11.97398
## [289] 11.96638 11.95825 11.94976 11.94096 11.93185 11.92248 11.91287 11.90304
## [297] 11.89303 11.88287 11.87257 11.86217 11.85171 11.84119 11.83066 11.82014
## [305] 11.80966 11.79924 11.78892 11.77872 11.76867 11.75880 11.74913 11.73970
## [313] 11.73053 11.72165 11.71308 11.70487 11.69702 11.68958 11.68256 11.67600
## [321] 11.66993 11.66437 11.65935 11.65490 11.65104 11.64781 11.64523 11.64334
## [329] 11.64214 11.64169 11.64200 11.64264 11.64320 11.64371 11.64420 11.64472
## [337] 11.64529 11.64596 11.64677 11.64774 11.64892 11.65033 11.65203 11.65404
## [345] 11.65640 11.65915 11.66231 11.66594 11.67007 11.67473 11.67995 11.68578
## [353] 11.69226 11.69941 11.70727 11.71589 11.72529 11.73516 11.74516 11.75531
## [361] 11.76564 11.77617 11.78693 11.79794 11.80923 11.82082 11.83273 11.84500
## [369] 11.85764 11.87068 11.88414 11.89805 11.91244 11.92732 11.94272 11.95866
## [377] 11.97518 11.99229 12.01003 12.02840 12.04733 12.06672 12.08655 12.10684
## [385] 12.12757 12.14875 12.17039 12.19247 12.21499 12.23796 12.26138 12.28525
## [393] 12.30955 12.33430 12.35950 12.38514 12.41121 12.43773 12.46469 12.49209
## [401] 12.51993 12.54821 12.57693 12.60608 12.63567 12.66569 12.69615 12.72705
## [409] 12.75837 12.79014 12.82233 12.85496 12.88801 12.92150 12.95542 12.98977
## [417] 13.02454 13.05974 13.09538 13.13143 13.16792 13.20483
#assign fits to a vector
both_trendb <- fit_bothb

#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax

#reassign dataframes (just to be safe)
work_bothb <- wrfb_both

#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date

#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
                    data = smooth_frame_bothb,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb,
                                  '</br> Median Log Copies: ', round(both_trendb, digits = 2)),
                    line = list(color = '#D95F02', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminb, digits = 2)),
                    name = "",
                    fillcolor = '#D95F02',
                    line = list(color = '#D95F02')) %>%
                layout(yaxis = list(title = "Total Log SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF B") %>%
    plotly::add_segments(x = as.Date("2020-06-24"), 
                                          xend = as.Date("2020-06-24"), 
                                          y = ~min(both_yminb), yend = ~max(both_ymaxb),
                                          opacity = 0.35,
                                          name = "Bars Repoen",
                                          hoverinfo = "text",
                                          text = "</br> Bars Reopen",
                                                 "</br> 2020-06-24",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-07-09"), 
                                          xend = as.Date("2020-07-09"), 
                                          y = ~min(both_yminb), yend = ~max(both_ymaxb),
                                          opacity = 0.35,
                                          name = "Mask Mandate",
                                          hoverinfo = "text",
                                          text = "</br> Mask Mandate",
                                                 "</br> 2020-07-09",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-08-20"), 
                                          xend = as.Date("2020-08-20"), 
                                          y = ~min(both_yminb), yend = ~max(both_ymaxb),
                                          opacity = 0.35,
                                          name = "</br> Classes Begin",
                                                 "</br> 2020-08-20",
                                          hoverinfo = "text",
                                          text = "Classes Begin",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
        plotly::add_segments(x = as.Date("2020-10-03"), 
                                          xend = as.Date("2020-10-03"), 
                                          y = ~min(both_yminb), yend = ~max(both_ymaxb),
                                          opacity = 0.35,
                                          name = "</br> First Home Football Game",
                                                 "</br> 2020-10-03",
                                          hoverinfo = "text",
                                          text = "First Home Football Game",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfb_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#D95F02', size = 6, opacity = 0.65))

p_wrf_b
save(p_wrf_b, file = "./plotly_objs/p_wrf_b.rda")

#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************

extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A', 
              span = 0.6, n = 422)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'

fit_bothc
##   [1] 11.96243 11.95876 11.95514 11.95157 11.94806 11.94461 11.94122 11.93789
##   [9] 11.93462 11.93141 11.92826 11.92517 11.92215 11.91919 11.91630 11.91347
##  [17] 11.91071 11.90801 11.90539 11.90283 11.90034 11.89792 11.89558 11.89331
##  [25] 11.89111 11.88898 11.88693 11.88495 11.88305 11.88123 11.87949 11.87782
##  [33] 11.87624 11.87473 11.87331 11.87196 11.87070 11.86953 11.86844 11.86743
##  [41] 11.86651 11.86568 11.86493 11.86427 11.86370 11.86323 11.86284 11.86254
##  [49] 11.86234 11.86223 11.86224 11.86240 11.86270 11.86314 11.86371 11.86441
##  [57] 11.86523 11.86618 11.86724 11.86842 11.86971 11.87110 11.87259 11.87418
##  [65] 11.87586 11.87763 11.87948 11.88141 11.88342 11.88550 11.88765 11.88987
##  [73] 11.89214 11.89447 11.89685 11.89928 11.90175 11.90426 11.90681 11.90939
##  [81] 11.91199 11.91462 11.91727 11.91993 11.92260 11.92528 11.92796 11.93064
##  [89] 11.93332 11.93598 11.93863 11.94126 11.94396 11.94678 11.94974 11.95282
##  [97] 11.95603 11.95935 11.96278 11.96631 11.96995 11.97368 11.97750 11.98141
## [105] 11.98540 11.98946 11.99360 11.99780 12.00206 12.00638 12.01075 12.01517
## [113] 12.01963 12.02412 12.02865 12.03320 12.03777 12.04236 12.04696 12.05157
## [121] 12.05617 12.06078 12.06538 12.06996 12.07453 12.07907 12.08358 12.08806
## [129] 12.09328 12.09990 12.10779 12.11680 12.12679 12.13763 12.14918 12.16128
## [137] 12.17380 12.18661 12.19955 12.21248 12.22527 12.23778 12.24986 12.26137
## [145] 12.27218 12.28213 12.29110 12.29893 12.30549 12.31255 12.32185 12.33322
## [153] 12.34645 12.36135 12.37775 12.39544 12.41424 12.43395 12.45440 12.47538
## [161] 12.49672 12.51821 12.53968 12.56092 12.58176 12.60200 12.62145 12.63992
## [169] 12.65723 12.67317 12.68757 12.70024 12.71098 12.71960 12.72592 12.73075
## [177] 12.73504 12.73881 12.74206 12.74482 12.74709 12.74888 12.75021 12.75110
## [185] 12.75155 12.75158 12.75121 12.75044 12.74928 12.74776 12.74588 12.74365
## [193] 12.74110 12.73823 12.73505 12.73159 12.72784 12.72383 12.71957 12.71507
## [201] 12.71034 12.70540 12.70026 12.69493 12.68943 12.68249 12.67298 12.66116
## [209] 12.64725 12.63150 12.61414 12.59543 12.57559 12.55487 12.53350 12.51173
## [217] 12.48979 12.46793 12.44637 12.42538 12.40517 12.38600 12.36810 12.35171
## [225] 12.33707 12.32442 12.31184 12.29737 12.28118 12.26348 12.24443 12.22423
## [233] 12.20306 12.18111 12.15855 12.13558 12.11238 12.08913 12.06602 12.04323
## [241] 12.02095 11.99937 11.97865 11.95900 11.94060 11.92363 11.90827 11.89365
## [249] 11.87879 11.86371 11.84844 11.83301 11.81745 11.80179 11.78604 11.77024
## [257] 11.75442 11.73860 11.72282 11.70709 11.69144 11.67591 11.66051 11.64529
## [265] 11.63026 11.61544 11.60088 11.58660 11.57262 11.55897 11.54567 11.53277
## [273] 11.52027 11.50819 11.49648 11.48514 11.47414 11.46347 11.45311 11.44304
## [281] 11.43325 11.42371 11.41441 11.40534 11.39648 11.38780 11.37930 11.37095
## [289] 11.36274 11.35465 11.34666 11.33875 11.33092 11.32314 11.31540 11.30772
## [297] 11.30014 11.29265 11.28529 11.27806 11.27100 11.26412 11.25744 11.25098
## [305] 11.24475 11.23878 11.23308 11.22768 11.22260 11.21785 11.21345 11.20942
## [313] 11.20578 11.20256 11.19976 11.19686 11.19336 11.18936 11.18495 11.18021
## [321] 11.17525 11.17015 11.16501 11.15991 11.15494 11.15021 11.14579 11.14179
## [329] 11.13828 11.13537 11.13315 11.13170 11.13112 11.13150 11.13293 11.13550
## [337] 11.13930 11.14373 11.14816 11.15263 11.15717 11.16183 11.16665 11.17166
## [345] 11.17691 11.18244 11.18828 11.19448 11.20108 11.20812 11.21564 11.22368
## [353] 11.23227 11.24147 11.25130 11.26182 11.27305 11.28476 11.29666 11.30878
## [361] 11.32113 11.33374 11.34662 11.35978 11.37325 11.38705 11.40118 11.41567
## [369] 11.43054 11.44581 11.46148 11.47758 11.49413 11.51115 11.52865 11.54664
## [377] 11.56516 11.58421 11.60381 11.62398 11.64463 11.66564 11.68703 11.70879
## [385] 11.73093 11.75346 11.77637 11.79968 11.82339 11.84749 11.87200 11.89692
## [393] 11.92225 11.94800 11.97417 12.00077 12.02779 12.05525 12.08315 12.11148
## [401] 12.14027 12.16953 12.19932 12.22960 12.26038 12.29165 12.32339 12.35561
## [409] 12.38828 12.42140 12.45497 12.48896 12.52339 12.55822 12.59346 12.62910
## [417] 12.66513 12.70154 12.73831 12.77545 12.81294 12.85077
#assign fits to a vector
both_trendc <- fit_bothc

#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax

#reassign dataframes (just to be safe)
work_bothc <- wrfc_both

#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date

#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
                    data = smooth_frame_bothc,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc,
                                  '</br> Median Log Copies: ', round(both_trendc, digits = 2)),
                    line = list(color = '#E7298A', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminc, digits = 2)),
                    name = "",
                    fillcolor = '#E7298A',
                    line = list(color = '#E7298A')) %>%
                layout(yaxis = list(title = "Total Log SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF C") %>%
    plotly::add_segments(x = as.Date("2020-06-24"), 
                                          xend = as.Date("2020-06-24"), 
                                          y = ~min(both_yminc), yend = ~max(both_ymaxc),
                                          opacity = 0.35,
                                          name = "Bars Repoen",
                                          hoverinfo = "text",
                                          text = "</br> Bars Reopen",
                                                 "</br> 2020-06-24",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-07-09"), 
                                          xend = as.Date("2020-07-09"), 
                                          y = ~min(both_yminc), yend = ~max(both_ymaxc),
                                          opacity = 0.35,
                                          name = "Mask Mandate",
                                          hoverinfo = "text",
                                          text = "</br> Mask Mandate",
                                                 "</br> 2020-07-09",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-08-20"), 
                                          xend = as.Date("2020-08-20"), 
                                          y = ~min(both_yminc), yend = ~max(both_ymaxc),
                                          opacity = 0.35,
                                          name = "</br> Classes Begin",
                                                 "</br> 2020-08-20",
                                          hoverinfo = "text",
                                          text = "Classes Begin",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
        plotly::add_segments(x = as.Date("2020-10-03"), 
                                          xend = as.Date("2020-10-03"), 
                                          y = ~min(both_yminc), yend = ~max(both_ymaxc),
                                          opacity = 0.35,
                                          name = "</br> First Home Football Game",
                                                 "</br> 2020-10-03",
                                          hoverinfo = "text",
                                          text = "First Home Football Game",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfc_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#E7298A', size = 6, opacity = 0.65))

p_wrf_c
save(p_wrf_c, file = "./plotly_objs/p_wrf_c.rda")
save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
save(both_ymina, file = "./plotly_objs/both_ymina.rda")
save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")

save(both_yminb, file = "./plotly_objs/both_yminb.rda")
save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")

save(both_yminc, file = "./plotly_objs/both_yminc.rda")
save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")